#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "DataHelper.h"

#include "QMutex"
#include <QLocalServer>
#include <QLocalSocket>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    int createTab(QString name = "");

protected:
    virtual void keyPressEvent(QKeyEvent *event);
    virtual void keyReleaseEvent(QKeyEvent *event);

private:
    Ui::MainWindow *ui;

    QMutex mutex;
    QVector<Tab*> tabs;

    Tab* getTabById    (int tabId = -1);
    Tab* getTabByWidget(QWidget *w);

    int  tabFocus      (int tabId);
    bool deleteTab     (Tab* tab);
    bool isTabExist    (int tabId);
    bool isTabNameExist(QString tabName);

    void StartServer(const QString&);
    void getNewConnections();

    QLocalServer server;
    QVector<QLocalSocket*> connection_list;

    QString version = "v 1.35";

    QPoint getPoint(QString *str);

    enum Commands : uint{
        undefined,
        IS_LOAD,
        GET_URL,
        OPEN_URL,
        SET_URL,
        TAB_FOCUS_BY_NAME,
        EVAL_JS,
        SET_PROXY,
        SET_USERAGENT,
        JS_ERROR_LOGGING,
        JS_LOG_LEVEL,
        JS_ON_ERROR_ALERT,
        GET_HTML,
        SET_HTML,
        V2,
        V3,
        HCAP,
        RECAPTCHA_RESPONSE,
        FORWARD,
        BACK,
        RELOAD,
        GET_IDS,
        GET_USERAGENT,
        GET_TAB_ID,
        GET_TAB_COUNT,
        CLOSE_WINDOW,
        WINDOW_MINIMAZE,
        WINDOW_RESTORE,
        ELEMENT_SET_VALUE_BY_ID,
        ELEMENT_SET_VALUE_BY_CLASS,
        ELEMENT_SET_INNER_HTML_BY_ID,
        ELEMENT_SET_INNER_HTML_BY_CLASS,
        ELEMENT_CLICK_BY_ID,
        ELEMENT_CLICK_BY_CLASS,
        DEL_TOR,
        PROXY_ON,
        PROXY_OFF,
        GET_COOKIES,
        DELETE_ALL_COOKIES,
        JS_ERROR,
        CLEAR_JS_ERRORS,
        ADD_TAB,
        ADD_TAB_FOCUS,
        CLOSE_TAB,
        TAB_FOCUS,
        CLOSE_TAB_BY_NAME,
        IS_TAB_EXISTS,
        IS_TAB_NAME_EXISTS,
        SET_WINDOW_SIZE,
        SET_POSITION,
        TOR,
        SET_ACCEPT_HEADER,
        SET_CACHE_DIR,
        SET_COOKIE_DIR,
        SET_OWN_CACHE_DIR,
        SET_OWN_COOKIE_DIR,
        SET_COOKIES,
        SET_COOKIES_FROM_STRING,
        SAVE_COOKIES,
        SAVE_COOKIES_TO_PATH,
        GET_COOKIE_VALUE_BY_NAME,
        GET_COOKIE_BY_DOMAIN,
        GET_DOMAIN_COOKIE_VALUE_BY_NAME,
        MOUSE_EMULATION,
        SCREEN_SHOT,
        PIXEL_EXISTS,
//        CONTAINS_IMAGE,
//        CAPMONSTER,
        L_BUTTON_CLICK,
        R_BUTTON_CLICK,
        L_BUTTON_PRESS,
        L_BUTTON_RELEASE,
        MOUSE_MOVE,
        SET_WINDOW_TITLE,
        SCROLL_Y,
        SCROLL_TO_Y,
        SCROLL_X,
        SCROLL_TO_X,
        SCROLL_Y_POSITION,
        SCROLL_Y_MAX,
        SCROLL_X_POSITION,
        SCROLL_X_MAX,
        GET_POSITION,
        GET_WINDOW_SIZE,
        IMAGES,
        IS_JS,
        ENABLE_JS,
        DISABLE_JS,
        WINDOW_MAXIMIZE,
        ENABLE_ACCELERATION,
        DISABLE_ACCELERATION,
        HIDE_BAR,
        SHOW_BAR,
        IS_BAR,
    };

    enum Errors : uint{
        NO_TABS,
        BAD_ID,
        NO_COMMAND,
        TAB_NOT_EXISTS,
        BAD_CAPTCHA_TYPE,
        BAD_COMMAND_NAME,
        BAD_PROXY_TYPE_OR_PARAMS,
        BAD_COMMAND_PARAM_LIST,
        BAD_COMMAND_NAME_OR_PARAMS,
        BAD_MOUSE_EMULATION,
        TOO_MUCH_PARAMS
    };
    const QByteArray NO_TABS_err                    = "No_Tabs_In_Browser";
    const QByteArray BAD_ID_err                     = "Bad_Tab_Id";
    const QByteArray NO_COMMAND_err                 = "Command_Name_Required";
    const QByteArray TAB_NOT_EXISTS_err             = "Tab_Not_Exists";
    const QByteArray BAD_CAPTCHA_TYPE_err           = "Bad_Captcha_Type";
    const QByteArray BAD_COMMAND_NAME_err           = "Command_Executor_Not_Found";
    const QByteArray BAD_PROXY_TYPE_OR_PARAMS_err   = "Bad_Proxy_Type_Or_Params";
    const QByteArray BAD_COMMAND_PARAM_LIST_err     = "Bad_Params_Count_For_Command";
    const QByteArray BAD_COMMAND_NAME_OR_PARAMS_err = "Bad_Command_Name_Or_Params";
    const QByteArray BAD_MOUSE_EMULATION_err        = "Mouse_Emulation_Failed";
    const QByteArray TOO_MUCH_PARAMS_err            = "Too_Much_Params_For_Command";
    const QByteArray UNDEFINED_ERROR                = "Undefined_Error";

    int mouseClick_X;
    int mouseClick_Y;    

    bool ControlKeyPressed = false;

    void save_position_and_pseudo_minimaze();
    void restore_position_and_show_normal();
    QPoint normal_pos;
    bool isMinimized = false;

    static void setCachePath(Tab *tab, QString path);
    static void setCookiePath(Tab *tab, QString path);

    void scrollToClickPoint(QPoint *point);
    bool performeMouseEmulation(Tab *tab, Commands command, QPoint *point);

    bool isMatchString(QString &string, QStringList strings);

    void SendToServer(QLocalSocket *socket, const QByteArray *str);
    void error(Errors err, QLocalSocket *socket);

    const QByteArray * getError(Errors err);
    Commands getCommand(QString *posterCommand);

private slots:    
    void readSocket();
    void disconnectSocket();

    void tabChanged(int index);

    bool eventFilter(QObject *obj, QEvent *event);

    void mouseMoveEvent (QMouseEvent* event);
    void mousePressEvent(QMouseEvent* event);

    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_url_line_returnPressed();    
    void on_btn_saveCookie_clicked();    
    void on_btn_removeCookie_clicked();
    void on_tabWidget_tabCloseRequested(int index);

};

#endif // MAINWINDOW_H
